<?php
Class System extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'system';
        $this->load->model('Msystem');
        
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 1, $this->session->userdata('admin_id'));
        if($this->session->userdata('admin') != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
    }
    
    
    public function permissionlist()
    {
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Msystem->getPermissionDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Msystem->deletePermissionDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/system/permission-list";</script>');
            }
        }
        
        $this->data['action'] = 'permissionlist';
        $this->load->view('template/layout', $this->data);
    }

    public function deletepermission()
    {
        $idArray = $this->getParamUri(4,1);
        $run = $this->Msystem->deletePermissionDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/system/permission-list";</script>');
        }
    }
    public function addpermission()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name_pm', 'Tên nhóm quyền', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->addPermissionDb($this->session->userdata('admin_id'));
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/system/permission-list";</script>');
            }
        }
        $this->data['action'] = 'addpermission';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editpermission()
    {
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name_pm', 'Tên nhóm quyền', 'required');
        
        $this->data['data'] = $this->Msystem->getPermissionDetailDb($this->data['id']);
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->editPermissionDb($this->session->userdata('admin_id'), $this->data['id']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/system/permission-list";</script>');
            }
        }
        $this->data['action'] = 'editpermission';
        $this->load->view('template/layout', $this->data);
    }
    
    public function permissionaccount()
    {
        $this->data['id_pg']        = $this->getParamUri(4,1);
        
        if(in_array($this->data['id_pg'], array(null, '', '0')))
        {
             $this->data['id_pg']   = $this->getParamInt('permission');     
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('checkSelect', 'Tài khoản quản trị', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $save = $this->Msystem->savePer($this->data['id_pg']);
        }
        
        $this->data['selectUser']   = $this->Msystem->selectUser(intval($this->input->post('user')));
        $this->data['module']       = $this->Msystem->getModule();
        $this->data['permission']   = $this->Msystem->selectPermission($this->data['id_pg']);
        $this->data['action'] = 'permissionaccount';
        $this->load->view('template/layout',$this->data);
    }
    
    /**
     * 
     * 
     * @void quản trị viên
     * 
     */
    
    
    public function listadmin()
    {
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Msystem->getListAdminDB($config['per_page'],$offset);
        //echo "<pre>"; die(print_r($data));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Msystem->deleteAdminDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        
        $this->data['action'] = 'listadmin';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addadmin()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên tài khoản', 'required|is_unique[user.username]');
        $this->form_validation->set_rules('pword', 'Mật khẩu', 'required|matches[pword2]');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        
        $this->data['typeAccount'] = $this->Msystem->selectTypeAccount(''); 
        
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->addAdminDb($this->session->userdata('admin_id'));
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/system/list-admin.html";</script>');
            }
        }
        $this->data['action'] = 'addadmin';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editadmin()
    {
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->data['data'] = $this->Msystem->getAdminid($this->data['id']);
        $this->data['typeAccount'] = $this->Msystem->selectTypeAccount($this->data['data']->level); 
        $this->data['permission_list'] = $this->Msystem->getPermissionList($this->data['id']);
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->editAdminDb($this->session->userdata('admin_id'), $this->data['id']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/system/list-admin.html";</script>');
            }
        }
        $this->data['action'] = 'editadmin';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteadmin()
    {
        $idArray = $this->getParamUri(4,1);
        $run = $this->Msystem->deleteAdminDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/system/list-admin";</script>');
        }
    }
    
    public function permissiongroup()
    {
        $this->checkAdmin($this->session->userdata('id_admin'), $this->session->userdata('permission_verify'));
        if($this->_per->read != 1 && ($this->session->userdata('admin') != 1))
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->load->helper('form');
        $this->data['data'] = $this->Msystem->getpermissiongroup();
        $this->data['action'] = 'permissiongroup';
        $this->load->view('temp/layout',$this->data);
    }
    
    public function permission()
    {
        
        $this->data['id_pg'] = $this->getParamInt('permission');
        
        if(!in_array($this->getParamString('savePermission'), array(null, '', '0')))
        {
            if(in_array($this->validationPer(), array(null, '', '0')))
            {
                $save = $this->Msystem->savePer($this->data['id_pg']);
            }else{
                $dataxs = '';
                foreach($this->validationPer() AS $le=>$el)
                {
                    $dataxs .= $el .'<br />';
                }
                $this->data['error'] = $dataxs;
            }
            
        }
        
        $this->data['selectUser'] = $this->Msystem->selectUser(intval($this->input->post('user')));
        $this->data['module'] = $this->Msystem->getModule();
        $this->data['permission'] = $this->Msystem->selectPermission($this->data['id_pg']);
        $this->data['action'] = 'permission';
        $this->load->view('temp/layout',$this->data);
    }
    
    private function validationPer()
    {
                
        $item = '';
        if(in_array($this->input->post('permission'), array(null, '', '0')))
        {
            $item[] = 'Vui lòng chọn nhóm quyền';
        }
        if(in_array($this->input->post('user'), array(null, '', '0')))
        {
            $item[] = 'Vui lòng chọn tài khoản';
        }
        if(in_array($this->input->post('module'), array(null, '', '0')))
        {
            $item[] = 'Vui lòng chọn ít nhất 1 Module';
        }
        
        return $item;
    } 
    
    
    public function getper()
    {
         $this->load->helper('form');
         $id_permission = $this->getParamInt('id_pg');
         $module = $this->getParamInt('module');
         $user = $this->getParamInt('user');
         $this->data['module'] = $this->Msystem->getPerModule($id_permission,$module,$user);
         
         $this->load->view('temp/user/getper',$this->data);
         
    }
    
    public function deletepermissionuser()
    {
        $id = $this->getParamInt('id');
        $this->db->delete('permission_account', "id = ".$id);
    }
    
    public function liststaff()
    {
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Msystem->getListStaffDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Msystem->deleteStaffDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/system/list-staff";</script>');
            }
        }
        
        $this->data['action'] = 'liststaff';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addstaff()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên tài khoản', 'required|is_unique[user.username]');
        $this->form_validation->set_rules('pword', 'Mật khẩu', 'required|matches[pword2]');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
		$this->form_validation->set_rules('restaurant_id', 'ID nhà hàng', 'required');
        
        //$this->data['typeAccount'] = $this->Msystem->selectTypeAccount(''); 
        $this->data['getListRestaurant'] = $this->Msystem->getListRestaurant(''); 
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->addStaffDb($this->session->userdata('admin_id'));
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/system/list-staff.html";</script>');
            }
        }
        $this->data['action'] = 'addstaff';
        $this->load->view('template/layout', $this->data);
    }
    
	public function deletestaff()
    {
        $idArray = $this->getParamUri(4,1);
        $run = $this->Msystem->deleteStaffDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/system/list-staff";</script>');
        }
    }
		
	public function editstaff()
    {
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Họ và tên', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->data['data'] = $this->Msystem->getStaffid($this->data['id']);
		$this->data['getListRestaurant'] = $this->Msystem->getListRestaurant($this->data['data']->id);
		//$this->data['typeAccount'] = $this->Msystem->selectTypeAccount($this->data['data']->level); 
        //$this->data['permission_list'] = $this->Msystem->getPermissionList($this->data['id']);
        if($this->form_validation->run() === TRUE)
        {
            $run = $this->Msystem->editStaffDb($this->session->userdata('admin_id'), $this->data['id']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/system/list-staff.html";</script>');
            }
        }
        $this->data['action'] = 'editstaff';
        $this->load->view('template/layout', $this->data);
    }	
	
}